<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input
        v-model="formData.name"
        :maxlength="50"
        placeholder="请输入数据标题，不超过50个字符"
      />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        :maxlength="200"
        @blur="specialFormValidate('desc')"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="尺寸" name="1">
        <el-form-item label="">
          <div class="row-item">
            <el-input
              v-model.number="formData.attr.scaleData"
              placeholder=""
              @blur="specialFormValidate('scale')"
              @keyup.enter.native="specialFormValidate('scale')"
            />
            <div class="unit">%</div>
          </div>
        </el-form-item>
      </el-collapse-item>
      <el-collapse-item title="位置" name="2">
        <el-form-item label="">
          <div class="row-item">
            <div class="title">经度</div>
            <el-input-number
              v-model="formData.attr.position[0]"
              placeholder=""
              :controls="false"
              :min="-180"
              :max="180"
              @blur="specialFormValidate('position')"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">°</div>
          </div>
          <div class="row-item">
            <div class="title">纬度</div>
            <el-input-number
              v-model="formData.attr.position[1]"
              placeholder=""
              :controls="false"
              :min="-90"
              :max="90"
              @blur="specialFormValidate('position')"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">°</div>
          </div>
          <div class="row-item">
            <div class="title">高度</div>
            <el-input-number
              v-model="formData.attr.position[2]"
              placeholder=""
              :controls="false"
              :precision="2"
              @blur="specialFormValidate('position')"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">米</div>
          </div>
        </el-form-item>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';
import { ValidateRule } from '@/views/enum/common';
import { notNumberInRange } from '@/utils';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const activeNames = ref<string[]>(['1', '2']);
const emits = defineEmits(['formValidate']);

function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['position'].includes(fieldName)) {
    formData.value.attr[fieldName].forEach((list: any, index: number) => {
      if (list === '' || isNaN(list)) {
        valid = false;
      }
      const min = ValidateRule[fieldName][index].min;
      const max = ValidateRule[fieldName][index].max;
      if (min && notNumberInRange(list, min, max)) {
        valid = false;
      }
    });
  } else if (['scale'].includes(fieldName)) {
    if (formData.value.attr.scale === '' || isNaN(formData.value.attr.scale)) {
      valid = false;
    } else {
      formData.value.attr.scale = formData.value.attr.scaleData / 100;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  emits('formValidate', fieldName, valid);
}

watchEffect(() => {
  formData.value = props.form;
  if (formData.value?.attr?.scale) {
    formData.value.attr.scaleData = formData.value.attr.scale * 100;
  }
});
</script>

<style lang="less" scoped>
.el-form {
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input,
      .el-input-number {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      &:last-child {
        margin-bottom: 0px;
      }
    }
  }
}
</style>
